package com.kedacom.ctsp.authority.service.simple;

import com.kedacom.ctsp.authority.dao.DepartmentRoleDao;
import com.kedacom.ctsp.authority.dao.UserRoleDao;
import com.kedacom.ctsp.authority.entity.*;
import com.kedacom.ctsp.authority.service.DepartmentService;
import com.kedacom.ctsp.authority.service.RoleService;
import com.kedacom.ctsp.authority.service.UserService;
import com.kedacom.ctsp.lang.Validate;
import com.kedacom.ctsp.lang.mapper.BeanMapper;
import com.kedacom.ctsp.web.entity.param.QueryParamEntity;
import com.kedacom.ctsp.web.service.StatusEnum;
import com.kedacom.ctsp.web.service.simple.AbstractCrudEntityService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 角色维护service
 *
 * @author
 */
@Service("authorityRoleService")
public class RoleServiceImpl extends AbstractCrudEntityService<Role, String> implements RoleService {

    @Autowired
    private UserRoleDao userRoleDao;
    @Autowired
    private DepartmentRoleDao departmentRoleDao;

    @Override
    public String insert(Role entity) {
        //Validate.isTrue(checkUnique(Role.SIGN, entity.getSign()), "auth.role.sign.duplicated");
        if (entity.getStatus() == null) {
            entity.setStatus(StatusEnum.ENABLE.ordinal());
        }
        String id = super.insert(entity);
        return id;
    }

    @Override
    public String patch(String roleId, Role entity) {
        Role oldEntity = get(roleId);
        Validate.notNull(oldEntity, "auth.role.notexist");
        // 修改了sign才需要检测
        if (StringUtils.isNotBlank(oldEntity.getSign()) && !oldEntity.getSign().equals(entity.getSign())) {
            // Validate.isTrue(checkUnique(Role.SIGN, entity.getSign()), "auth.role.sign.duplicated");
        }
        return super.patch(roleId, entity);
    }


    /**
     * 用户-角色映射
     *
     * @param users
     * @param rid
     */
    private void mergeUserRole(Collection<User> users, String rid) {
        QueryParamEntity q = new QueryParamEntity();
        q.and(UserRole.ROLE_ID, rid);
        userRoleDao.delete(q);

        List<UserRole> userRoles = users.stream().map(u -> {
                    UserRole userRole = BeanMapper.newInstance(UserRole.class).get();
                    userRole.setUserId(u.getId());
                    userRole.setRoleId(rid);
                    return userRole;
                }
        ).collect(Collectors.toList());
        userRoleDao.insert(userRoles);
    }

    /**
     * 部门-角色映射
     *
     * @param depts
     * @param rid
     */
    private void mergeDepartmentRole(Collection<Department> depts, String rid) {
        QueryParamEntity q = new QueryParamEntity();
        q.and(DepartmentRole.ROLE_ID, rid);
        departmentRoleDao.delete(q);

        List<DepartmentRole> drs = depts.stream().map(d -> {
                    DepartmentRole dr = BeanMapper.newInstance(DepartmentRole.class).get();
                    dr.setDepartmentId(d.getId());
                    dr.setRoleId(rid);
                    return dr;
                }
        ).collect(Collectors.toList());
        departmentRoleDao.insert(drs);
    }

    /**
     * 通过项目标识检查角色重复性
     *
     * @param name
     * @param clientSign
     * @param value
     * @return
     */
    @Override
    public boolean checkChildUnique(String name, String clientSign, String value) {

        Validate.notBlank(name, "unique column cannot be blank.");
        Validate.notBlank(value, "unique columne[%s]'s check value cannot be blank.", name);
        long total = createQuery().where(name, value).in(Role.CLIENTSIGN, clientSign).total();
        return !(total > 0);
    }



}
